package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginDemo {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名");
        String name=sc.nextLine();
        System.out.println("请输入密码");
        String password=sc.nextLine();
        try (Connection connection=DBUtil.getConnection()){
            Statement state=connection.createStatement();
            String sql="SELECT username,password,nickname,age "+
                        "FROM userinfo "+
                       "WHERE username='"+name+"' AND password='"+password+"'";
            ResultSet rs=state.executeQuery(sql);
            System.out.println(sql);
            if(rs.next()){
                String nickname=rs.getString("nickname");
                System.out.println("登录成功，欢迎"+nickname+"回来");
            }else {
                System.out.println("登录失败 用户名或密码不正确");
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
